﻿using System;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using Hongsh.Pay.Client.Proxy.Http;
using MapleLeaf.Pay;
using MapleLeaf.Pay.Pay;
using MapleLeaf.Pay.Trade;

namespace Hongsh.Pay.Client.Proxy.Handlers {
    // Token: 0x02000062 RID: 98
    public static class PospalPayProxy {
        // Token: 0x0600032C RID: 812 RVA: 0x00004054 File Offset: 0x00002254
        public static void AddPospalPayProxyHandler(this HttpServer svr) {
            svr.RouterList.Add("POST/pos/v1/payment/PayOnline", new HttpContextActionDelegate(Process));
            svr.RouterList.Add("POST/pos/v1/UnifiedPayment/PosScanClient", new HttpContextActionDelegate(Process));
        }

        // Token: 0x0600032D RID: 813 RVA: 0x0000E9A0 File Offset: 0x0000CBA0
        private static void Process(HttpContext ctx) {
            string json = new StreamReader(ctx.Request).ReadToEnd();
            PospalPayPaymentRequest pospalPayPaymentRequest = json.JsonDecode<PospalPayPaymentRequest>();
            string status = "success";
            string[] messages = null;
            PospalPayPaymentResult result = null;
            try {
                HongshPayTrade hongshPayTrade = null;
                if (string.IsNullOrEmpty(pospalPayPaymentRequest.ticketUid)) {
                    PospalPayExtraData pospalPayExtraData = pospalPayPaymentRequest.extraData.JsonDecode<PospalPayExtraData>();
                    pospalPayPaymentRequest.ticketUid = pospalPayExtraData.uid.ToString();
                }
                string tradeId;
                if (!TradeMap.TryGetValue(pospalPayPaymentRequest.ticketUid, out tradeId)) {
                    DateTime now = DateTime.Now;
                    TradeAuthPaymentRequest tradeAuthPaymentRequest = new TradeAuthPaymentRequest {
                        ClientTradeId = pospalPayPaymentRequest.ticketUid,
                        Amount = (int)(pospalPayPaymentRequest.totalAmount * 100m),
                        Authcode = pospalPayPaymentRequest.code
                    };
                    try {
                        tradeAuthPaymentRequest.GoodsList = (from d in pospalPayPaymentRequest.products
                                                             select new TradeGoods {
                                                                 Id = d.barcode,
                                                                 Name = d.name,
                                                                 Price = (int)(d.price * 100m),
                                                                 Quantity = d.quantity
                                                             }).ToArray<TradeGoods>();
                    }
                    catch {
                    }
                    TradeMap[pospalPayPaymentRequest.ticketUid] = null;
                    try {
                        hongshPayTrade = ctx.Execute(tradeAuthPaymentRequest);
                        TradeMap[pospalPayPaymentRequest.ticketUid] = hongshPayTrade.TradeId;
                        goto IL_15D;
                    }
                    catch (Exception ex) {
                        TradeMap.TryRemove(pospalPayPaymentRequest.ticketUid, out tradeId);
                        throw ex;
                    }
                }
                hongshPayTrade = ctx.Execute(new TradePaymentQueryRequest {
                    TradeId = tradeId,
                    ClientTradeId = pospalPayPaymentRequest.ticketUid
                });
            IL_15D:
                if (hongshPayTrade.State == TradeState.失败 || hongshPayTrade.State == TradeState.关闭) {
                    throw new Exception(hongshPayTrade.StateDescript);
                }
                if (hongshPayTrade.State == TradeState.成功) {
                    result = new PospalPayPaymentResult {
                        externalOrderNo = hongshPayTrade.TradeId,
                        localOrderNo = hongshPayTrade.TradeId,
                        buyerPayAmount = hongshPayTrade.ActualAmount,
                        receiptAmount = hongshPayTrade.Amount
                    };
                }
                else {
                    ctx.Connection.Close();
                }
            }
            catch (Exception ex2) {
                status = "error";
                messages = new string[]
                {
                    ex2.Message
                };
            }
            ctx.Result = new HttpJsonResult(new {
                status,
                messages,
                result
            });
        }

        // Token: 0x0600032E RID: 814 RVA: 0x0000408E File Offset: 0x0000228E
        static PospalPayProxy() {
            //Class15.cwcZ3SWztHroi();
            TradeMap = new ConcurrentDictionary<string, string>();
        }

        // Token: 0x04000159 RID: 345
        private static readonly ConcurrentDictionary<string, string> TradeMap;

        // Token: 0x02000063 RID: 99
        public class PospalPayGoods {
            // Token: 0x1700015F RID: 351
            // (get) Token: 0x0600032F RID: 815 RVA: 0x0000409F File Offset: 0x0000229F
            // (set) Token: 0x06000330 RID: 816 RVA: 0x000040A7 File Offset: 0x000022A7
            public string barcode { get; set; }

            // Token: 0x17000160 RID: 352
            // (get) Token: 0x06000331 RID: 817 RVA: 0x000040B0 File Offset: 0x000022B0
            // (set) Token: 0x06000332 RID: 818 RVA: 0x000040B8 File Offset: 0x000022B8
            public string name { get; set; }

            // Token: 0x17000161 RID: 353
            // (get) Token: 0x06000333 RID: 819 RVA: 0x000040C1 File Offset: 0x000022C1
            // (set) Token: 0x06000334 RID: 820 RVA: 0x000040C9 File Offset: 0x000022C9
            public decimal price { get; set; }

            // Token: 0x17000162 RID: 354
            // (get) Token: 0x06000335 RID: 821 RVA: 0x000040D2 File Offset: 0x000022D2
            // (set) Token: 0x06000336 RID: 822 RVA: 0x000040DA File Offset: 0x000022DA
            public double quantity { get; set; }

            // Token: 0x06000337 RID: 823 RVA: 0x0000286D File Offset: 0x00000A6D
            public PospalPayGoods() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }

        // Token: 0x02000064 RID: 100
        public class PospalPayPaymentRequest {
            // Token: 0x17000163 RID: 355
            // (get) Token: 0x06000338 RID: 824 RVA: 0x000040E3 File Offset: 0x000022E3
            // (set) Token: 0x06000339 RID: 825 RVA: 0x000040EB File Offset: 0x000022EB
            public string ticketUid { get; set; }

            // Token: 0x17000164 RID: 356
            // (get) Token: 0x0600033A RID: 826 RVA: 0x000040F4 File Offset: 0x000022F4
            // (set) Token: 0x0600033B RID: 827 RVA: 0x000040FC File Offset: 0x000022FC
            public string cashierUid { get; set; }

            // Token: 0x17000165 RID: 357
            // (get) Token: 0x0600033C RID: 828 RVA: 0x00004105 File Offset: 0x00002305
            // (set) Token: 0x0600033D RID: 829 RVA: 0x0000410D File Offset: 0x0000230D
            public string code { get; set; }

            // Token: 0x17000166 RID: 358
            // (get) Token: 0x0600033E RID: 830 RVA: 0x00004116 File Offset: 0x00002316
            // (set) Token: 0x0600033F RID: 831 RVA: 0x0000411E File Offset: 0x0000231E
            public string extraData { get; set; }

            // Token: 0x17000167 RID: 359
            // (get) Token: 0x06000340 RID: 832 RVA: 0x00004127 File Offset: 0x00002327
            // (set) Token: 0x06000341 RID: 833 RVA: 0x0000412F File Offset: 0x0000232F
            public decimal totalAmount { get; set; }

            // Token: 0x17000168 RID: 360
            // (get) Token: 0x06000342 RID: 834 RVA: 0x00004138 File Offset: 0x00002338
            // (set) Token: 0x06000343 RID: 835 RVA: 0x00004140 File Offset: 0x00002340
            public PospalPayGoods[] products { get; set; }

            // Token: 0x06000344 RID: 836 RVA: 0x0000286D File Offset: 0x00000A6D
            public PospalPayPaymentRequest() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }

        // Token: 0x02000065 RID: 101
        public class PospalPayExtraData {
            // Token: 0x17000169 RID: 361
            // (get) Token: 0x06000345 RID: 837 RVA: 0x00004149 File Offset: 0x00002349
            // (set) Token: 0x06000346 RID: 838 RVA: 0x00004151 File Offset: 0x00002351
            public int uid { get; set; }

            // Token: 0x1700016A RID: 362
            // (get) Token: 0x06000347 RID: 839 RVA: 0x0000415A File Offset: 0x0000235A
            // (set) Token: 0x06000348 RID: 840 RVA: 0x00004162 File Offset: 0x00002362
            public int customerUid { get; set; }

            // Token: 0x1700016B RID: 363
            // (get) Token: 0x06000349 RID: 841 RVA: 0x0000416B File Offset: 0x0000236B
            // (set) Token: 0x0600034A RID: 842 RVA: 0x00004173 File Offset: 0x00002373
            public decimal rechargeMoney { get; set; }

            // Token: 0x1700016C RID: 364
            // (get) Token: 0x0600034B RID: 843 RVA: 0x0000417C File Offset: 0x0000237C
            // (set) Token: 0x0600034C RID: 844 RVA: 0x00004184 File Offset: 0x00002384
            public decimal giftMoney { get; set; }

            // Token: 0x1700016D RID: 365
            // (get) Token: 0x0600034D RID: 845 RVA: 0x0000418D File Offset: 0x0000238D
            // (set) Token: 0x0600034E RID: 846 RVA: 0x00004195 File Offset: 0x00002395
            public decimal giftPoint { get; set; }

            // Token: 0x1700016E RID: 366
            // (get) Token: 0x0600034F RID: 847 RVA: 0x0000419E File Offset: 0x0000239E
            // (set) Token: 0x06000350 RID: 848 RVA: 0x000041A6 File Offset: 0x000023A6
            public string cashierUid { get; set; }

            // Token: 0x1700016F RID: 367
            // (get) Token: 0x06000351 RID: 849 RVA: 0x000041AF File Offset: 0x000023AF
            // (set) Token: 0x06000352 RID: 850 RVA: 0x000041B7 File Offset: 0x000023B7
            public string payMethod { get; set; }

            // Token: 0x17000170 RID: 368
            // (get) Token: 0x06000353 RID: 851 RVA: 0x000041C0 File Offset: 0x000023C0
            // (set) Token: 0x06000354 RID: 852 RVA: 0x000041C8 File Offset: 0x000023C8
            public int payMethodCode { get; set; }

            // Token: 0x17000171 RID: 369
            // (get) Token: 0x06000355 RID: 853 RVA: 0x000041D1 File Offset: 0x000023D1
            // (set) Token: 0x06000356 RID: 854 RVA: 0x000041D9 File Offset: 0x000023D9
            public string datetime { get; set; }

            // Token: 0x17000172 RID: 370
            // (get) Token: 0x06000357 RID: 855 RVA: 0x000041E2 File Offset: 0x000023E2
            // (set) Token: 0x06000358 RID: 856 RVA: 0x000041EA File Offset: 0x000023EA
            public int rechargeType { get; set; }

            // Token: 0x17000173 RID: 371
            // (get) Token: 0x06000359 RID: 857 RVA: 0x000041F3 File Offset: 0x000023F3
            // (set) Token: 0x0600035A RID: 858 RVA: 0x000041FB File Offset: 0x000023FB
            public int rechargeChannelCode { get; set; }

            // Token: 0x17000174 RID: 372
            // (get) Token: 0x0600035B RID: 859 RVA: 0x00004204 File Offset: 0x00002404
            // (set) Token: 0x0600035C RID: 860 RVA: 0x0000420C File Offset: 0x0000240C
            public string account { get; set; }

            // Token: 0x0600035D RID: 861 RVA: 0x0000286D File Offset: 0x00000A6D
            public PospalPayExtraData() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }

        // Token: 0x02000066 RID: 102
        public class PospalPayPaymentResult {
            // Token: 0x17000175 RID: 373
            // (get) Token: 0x0600035E RID: 862 RVA: 0x00004215 File Offset: 0x00002415
            // (set) Token: 0x0600035F RID: 863 RVA: 0x0000421D File Offset: 0x0000241D
            public string externalOrderNo { get; set; }

            // Token: 0x17000176 RID: 374
            // (get) Token: 0x06000360 RID: 864 RVA: 0x00004226 File Offset: 0x00002426
            // (set) Token: 0x06000361 RID: 865 RVA: 0x0000422E File Offset: 0x0000242E
            public string localOrderNo { get; set; }

            // Token: 0x17000177 RID: 375
            // (get) Token: 0x06000362 RID: 866 RVA: 0x00004237 File Offset: 0x00002437
            // (set) Token: 0x06000363 RID: 867 RVA: 0x0000423F File Offset: 0x0000243F
            public decimal receiptAmount { get; set; }

            // Token: 0x17000178 RID: 376
            // (get) Token: 0x06000364 RID: 868 RVA: 0x00004248 File Offset: 0x00002448
            // (set) Token: 0x06000365 RID: 869 RVA: 0x00004250 File Offset: 0x00002450
            public decimal buyerPayAmount { get; set; }

            // Token: 0x06000366 RID: 870 RVA: 0x0000286D File Offset: 0x00000A6D
            public PospalPayPaymentResult() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }
    }
}
